//
//  EditInfoViewController.m
//  QT
//
//  Created by 甘世清 on 2025/1/7.
//

#import "EditInfoViewController.h"
#import "EditInfoTopView.h"
#import "EditInfoTableViewCell.h"
#import "EditInfoModel.h"
#import "EditNameViewController.h"
#import "XiangCeViewController.h"
#import "EditAboutViewController.h"
@interface EditInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong)EditInfoTopView * topView;
@end

@implementation EditInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle= SQNSLocalString(@"编辑资料");
    self.zx_navBarBackgroundColor = UIColor.whiteColor;
    self.view.backgroundColor = SQColorF1F1F1;
    // Do any additional setup after loading the view.
}

- (void)SQ_UIViewForSelfView{
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.tableHeaderView = self.topView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
  
}
- (void)SQ_viewWillAppear:(BOOL)animated{
    [self SQ_Header];
}

- (void)SQ_Header{
    [HttpTool post:@"user/info" paramDic:[NSMutableDictionary new] successBlock:^(BaseModel * _Nonnull httpModel) {
        [UserModel SQ_updateLocal:httpModel];
        [EditInfoModel SQ_jsonArray:self.jsonArray];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.jsonArray.count ;
}

 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EditInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditInfoTableViewCell"];
    if (!cell) {
        cell = [[EditInfoTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"EditInfoTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [EditInfoTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:0];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EditInfoModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    if ([model.leftString isEqualToString:@"昵称"]) {
        EditNameViewController * vc = [[EditNameViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    } else  if ([model.leftString isEqualToString:@"城市"]) {
        [YJProgressHUD SQ_showMsgWithoutView:@""];
        WS(weakSelf);
        [[SQCLLocationManager sharedManager]SQ_Location:^(NSDictionary * _Nonnull cityDic) {
            [weakSelf SQ_city:cityDic];
        }];
    }else  if ([model.leftString isEqualToString:@"相册"]) {
        XiangCeViewController * vc = [[XiangCeViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }else  if ([model.leftString isEqualToString:@"简介"]) {
        EditAboutViewController * vc = [[EditAboutViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }
}
-(void)SQ_city:(NSDictionary*)dic{
    WS(weakSelf);
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool post:@"user/config" paramDic:[[NSMutableDictionary alloc]initWithDictionary:dic] successBlock:^(BaseModel * _Nonnull httpModel) {
        [weakSelf SQ_Header];
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"定位成功")];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
       [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (EditInfoTopView *)topView{
    if (!_topView) {
        _topView = [[EditInfoTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 200)];
    }
    return _topView;
}
@end
